package com.neuedu.wemall.seller.web.Intereptor;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.wemall.common.util.RedisUtils;
import com.neuedu.wemall.common.vo.ResponseData;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.IOException;
import java.io.PrintWriter;

import static com.alibaba.fastjson.serializer.SerializerFeature.*;

/**
 * 登录检查拦截器
 */
@Log4j2
@Component
public class LoginCheckedInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisUtils redis;
    //令牌失效时间
    @Value("${config.token.expire}")
    private int expire;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String url = request.getRequestURI();
        log.debug("当前请求路径为:" + url);
        return true;
    }

    /**
     * 向客户端输出错误提示信息
     *
     * @param data
     */
    private void printMessage(ResponseData data, HttpServletResponse response) throws IOException {
        //向客户端输出JSON格式的数据
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = null;
        out = response.getWriter();
        String printText = JSONObject.toJSONStringWithDateFormat(data, "yyyy-MM-dd HH:mm:ss", WriteNullBooleanAsFalse,
                WriteNullListAsEmpty,
                WriteNullStringAsEmpty,
                WriteNullNumberAsZero,
                WriteDateUseDateFormat,
                WriteMapNullValue,
                PrettyFormat,
                DisableCircularReferenceDetect,
                BrowserCompatible);
        out.print(printText);
    }
}
